<template>
    <div>
        <el-header>借款初审</el-header>
        <el-row>
           <br>
        </el-row>
        <el-row>
            <el-col :span="20" :offset="2">
                <el-row style="background-color: #E4E7ED;height: 40px;">
                    <el-col :span="3" style="margin-top: 5px;">
                        <i class="el-icon-search"
                            style="font-size: 20px;"></i>
                        <span style="font-size: 20px;">精确查询</span>
                    </el-col>
                    <el-col :span="1" :offset="17" style="font-size: 20px;">
                        <el-button icon="el-icon-search" type="success" @click="searchProject">查询</el-button>
                    </el-col>
                    <el-col :span="1" :offset="1" style="font-size: 20px;">
                        <el-button icon="el-icon-refresh-right" type="success" @click="reset" plain>重置</el-button>
                    </el-col>
                </el-row>
                <el-row style="border: 1px solid gray;height: 80px;">
                    <el-col :span="4" :offset="1" style="margin-top: 20px;">
                        <el-input placeholder="用户名" v-model="search.uname" clearable></el-input>
                    </el-col>
                    <el-col :span="4" :offset="2" style="margin-top: 20px;">
                        <el-input placeholder="项目名称" v-model="search.pname" clearable></el-input>
                    </el-col>
                    <el-col :span="4" :offset="2" style="margin-top: 20px;">
                        <el-input placeholder="借款人名" v-model="search.name" clearable></el-input>
                    </el-col>
                    <el-col :span="4" :offset="2" style="margin-top: 20px;">
                        <el-input placeholder="借款金额" v-model="search.money" clearable></el-input>
                    </el-col>
                </el-row>
                <el-row style="background-color: #E4E7ED;height: 40px;margin-top: 40px;">
                    <el-col :span="3" style="margin-top: 8px;">
                        <i class="el-icon-document"
                            style="font-size: 20px;"></i>
                        <span>数据记录</span>
                    </el-col>
                    <el-col :span="1" :offset="15" style="font-size: 20px;">
                        <el-button type="success" @click="addProject">发布项目</el-button>
                    </el-col>
                    <el-col :span="1" :offset="2" style="font-size: 20px;">
                        <el-button icon="el-icon-document-copy" type="success">导出表格</el-button>
                    </el-col>
                </el-row>
                <el-row>
                    <el-table :data="projects" style="width: 100%" >
                        <el-table-column prop="pid" label="编号" align="center" width="80">
                        </el-table-column>
                        <el-table-column prop="pno" label="序号" align="center" width="150">
                        </el-table-column>
                        <el-table-column prop="uname" label="用户名" align="center" width="80">
                        </el-table-column>
                        <el-table-column prop="name" label="借款人姓名" align="center" width="100">
                        </el-table-column>
                        <el-table-column prop="pname" label="项目名称" align="center" width="100">
                        </el-table-column>
                        <el-table-column prop="type" label="项目类型" align="center" width="100">
                        </el-table-column>
                        <el-table-column prop="money" label="借款金额" align="center" width="100">
                        </el-table-column>
                        <el-table-column prop="rate" label="年化利率" align="center" width="100">
                            <template slot-scope="scope">
                                {{ formatToPercentage(scope.row.rate) }}
                            </template>
                        </el-table-column>
                        <el-table-column prop="deadline" label="借款期限" align="center" width="100">
                        </el-table-column>
                        <el-table-column prop="addtime" value-format="yyyy-MM-dd HH:mm:ss" label="添加时间" align="center"
                            width="170">
                        </el-table-column>
                        <el-table-column label="操作" width="250" align="center" fixed="right">
                            <template slot-scope="scope">
                                <el-button type="danger" icon="el-icon-search" @click="handleCheck(scope.row)"></el-button>
                                <el-button type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)"></el-button>
                                <el-button type="success" icon="el-icon-check" @click="handleApproved(scope.row)"></el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-row>
                <el-row>
                    <div style="text-align: center">
                        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                            :current-page="page" :page-sizes="[5, 10]" :page-size="size"
                            layout="total, sizes, prev, pager, next, jumper" :total="total">
                        </el-pagination>
                    </div>
                </el-row>
            </el-col>
        </el-row>
        <el-row>
            <el-dialog title="项目详情" :visible.sync="dialogFormVisible">
                <el-form :model="addProjectForm">
                    <el-row>
                        <el-col :offset="1">
                            <span class="title">标的设置</span>
                        </el-col>
                    </el-row>
                    <br/>
                    <el-row>
                        <el-col :span="10">
                            <el-form-item label="贷款类型" :label-width="formLabelWidth">
                                <el-select v-model="addProjectForm.type" placeholder="请选择贷款类型" :disabled="enableStatus">
                                    <el-option label="车贷" value="车贷"></el-option>
                                    <el-option label="房贷" value="房贷"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="项目名称" :label-width="formLabelWidth">
                                <el-input 
                                    v-model="addProjectForm.pname" 
                                    autocomplete="off"
                                    placeholder="请输入项目名称"
                                    :disabled="enableStatus"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :offset="1">
                            <span class="title">基本信息设置（必填项）</span>
                        </el-col>
                    </el-row>
                    <br/>
                    <el-row>
                        <el-col :span="10">
                            <el-form-item label="借款金额" :label-width="formLabelWidth">
                                <el-input v-model="addProjectForm.money" autocomplete="off" :disabled="enableStatus"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="借款人" :label-width="formLabelWidth">
                                <!-- <el-input v-model="addProjectForm.uname" autocomplete="off"></el-input> -->
                                <template>
                                    <el-select v-model="addProjectForm.uid" placeholder="请选择借款人" 
                                            clearable
                                            :disabled="enableStatus">
                                        <el-option
                                            v-for="item in borrowerList"
                                            :key="item.id"
                                            :label="item.name"
                                            :value="item.id">
                                        </el-option>
                                    </el-select>
                                </template>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="10">
                            <el-form-item label="还款方式" :label-width="formLabelWidth">
                                <el-select v-model="addProjectForm.repayment" 
                                        placeholder="请选择还款类型类型" 
                                        :disabled="enableStatus">
                                    <el-option label="一次性还本付息" value="一次性还本付息"></el-option>
                                    <el-option label="先息后本" value="先息后本"></el-option>
                                    <el-option label="等额本息" value="等额本息"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item 
                                    label="借款期限" 
                                    :label-width="formLabelWidth">
                                <el-input v-model="addProjectForm.deadline" autocomplete="off" :disabled="enableStatus">
                                    <i slot="suffix">月</i>
                                </el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="10">
                            <el-form-item label="年利化率" :label-width="formLabelWidth">
                                <el-input v-model="addProjectForm.rate" autocomplete="off" :disabled="enableStatus">
                                    <i slot="suffix">%</i>
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="开标时间" :label-width="formLabelWidth">
                                <el-date-picker
                                  v-model="addProjectForm.starttime"
                                  value-format="yyyy-MM-dd HH:mm:ss"
                                  type="datetime"
                                  placeholder="选择日期时间"
                                  :disabled="enableStatus">
                                </el-date-picker>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="10">
                            <el-form-item label="最低投资数" :label-width="formLabelWidth" :disabled="enableStatus">
                                <el-input v-model="addProjectForm.minmoney" autocomplete="off" :disabled="enableStatus"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="募集期" :label-width="formLabelWidth">
                                <el-input v-model="addProjectForm.collecttime" autocomplete="off" :disabled="enableStatus">
                                    <i slot="suffix">天</i>
                                </el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :offset="1">
                            <span class="title">其他特殊设置（非必填项）</span>
                        </el-col>
                    </el-row>
                    <br/>
                    <el-row>
                        <el-col :span="10">
                            <el-form-item label="担保人" :label-width="formLabelWidth">
                                <!-- <el-input v-model="addProjectForm.gname" autocomplete="off"></el-input> -->
                                <template>
                                    <el-select v-model="addProjectForm.gid" placeholder="请选择担保人" 
                                            clearable
                                            :disabled="enableStatus">
                                        <el-option
                                            v-for="item in guaranteeList"
                                            :key="item.id"
                                            :label="item.name"
                                            :value="item.id">
                                        </el-option>
                                    </el-select>
                                </template>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="奖励比例" :label-width="formLabelWidth">
                                <el-input v-model="addProjectForm.bonusrate" autocomplete="off" :disabled="enableStatus">
                                    <i slot="suffix">%</i>
                                </el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="10">
                            <el-form-item label="放款管理费率" :label-width="formLabelWidth">
                                <el-input v-model="addProjectForm.loanrate" autocomplete="off" :disabled="enableStatus">
                                    <i slot="suffix">%</i>
                                </el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="cancel">取 消</el-button>
                    <el-button type="primary" @click="submit">{{submitTittle}}</el-button>
                </div>
            </el-dialog>
        </el-row>
    </div>
</template>
<script>
export default {
    data(){
        return{
            search:{
                uname:"",
                pname:"",
                name:"",
                money:"",
                page: 1,
                size: 5,
            },
            projects:[],
            page: 1,
            size: 5,
            total: 0,
            totalPage: 0,
            projectForm:{},
            dialogFormVisible: false,
            addProjectForm:{},
            formLabelWidth: '120px',
            submitTittle:"",
            borrowerList:[],
            guaranteeList:[],
            enableStatus:false,
        };
    },
    methods: {
        searchProject(){
            this.$axios.post("/project/searchProject",this.search).then(res => {
                if(res.data.code == 200){
                    this.projects = res.data.data.data;
                    this.total = res.data.data.total;
                    this.totalPage = res.data.data.totalPage;
                }
            })
        },
        reset(){
            this.search = {
                uname:"",
                pname:"",
                name:"",
                money:"",
                page: 1,
                size: 5,
            };
        },
        addProject(){
            this.enableStatus = false;
            this.submitTittle = "发 布"
            this.dialogFormVisible = true;
        },
        handleCheck(row){
            this.enableStatus = true;
            this.dialogFormVisible = true;
            this.checkDetail(row);
        },
        handleEdit(row){
            this.enableStatus = false;
            this.submitTittle = "修 改";
            this.dialogFormVisible = true;
            this.checkDetail(row);
        },
        handleApproved(row){
            console.log(row);
            this.$axios.post("/project/passPreliminaryExamination",row).then(res => {
                if(res.data.code == 200){
                    this.$message({
                        message: '添加成功!',
                        type: 'success'
                    });
                    this.searchProject();
                }
            })
        },
        //  提交表单
        submit(){
            if(this.submitTittle == "发 布"){
                console.log("添加项目");
                console.log(this.addProjectForm);
                this.$axios.post("/project/addProject",this.addProjectForm).then(res => {
                    if(res.data.code == 200){
                        this.$message({
                            message: '添加成功!',
                            type: 'success'
                        });
                        this.addProjectForm={};
                        this.dialogFormVisible = false;
                        this.searchProject();
                    }
                })
            }else{
                console.log("修改项目");
                console.log(this.addProjectForm);
                this.$axios.put("/project/updateProject",this.addProjectForm).then(res => {
                    if(res.data.code == 200){
                        this.$message({
                            message: '修改成功!',
                            type: 'success'
                        });
                        this.addProjectForm={};
                        this.dialogFormVisible = false;
                        this.searchProject();
                    }
                })
            }
        },
        cancel(){
            this.dialogFormVisible = false;
            this.addProjectForm = {};
        },
        checkDetail(row){
            console.log("查看详情");
            this.$axios.get("/project/checkDetail/" + row.pno).then(res => {
                console.log(res);
                this.addProjectForm = res.data.data
            })
        },
        handleSizeChange(val) {
            this.search.size = val;
            this.searchProject();
        },
        handleCurrentChange(val) {
            this.search.page = val;
            this.searchProject();
        },
        formatToPercentage(row){
            let rate = row * 100 + "%"
            return rate;
        },
    },mounted() {
        this.searchProject();
        this.$axios.get("/user/findBorrowerAndGuarantee").then(res => {
            console.log(res);
            this.borrowerList = res.data.data.borrowerList;
            this.guaranteeList = res.data.data.guaranteeList;
        })
    },
}
</script>
<style scoped>
.title{
    color:#67C23A;
}
.el-header {
    background-color: #67C23A;
    color: #333;
    text-align: center;
    line-height: 60px;
  }
</style>